package Gui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import controller.CatalogControl;
import controller.MarketingTemplateControl;
import controller.ProductControl;

import Client_Server.ClientConsole;
import Entity.Catalog;
import Entity.Template;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;



// TODO: Auto-generated Javadoc
/**
 * The Class UpdateCatalog.
 */
public class UpdateCatalog extends JPanel{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6400283674228776823L;
	
	/** The Product id label. */
	public static JLabel ProductIDLabel;
	
	/** The Product name label. */
	public static JLabel ProductNameLabel;
	
	/** The Product color label. */
	public static JLabel ProductColorLabel;
	
	/** The Product size label. */
	public static JLabel ProductSizeLabel;
	
	/** The Product use label. */
	public static JLabel ProductUseLabel;
	
	/** The Product price label. */
	public static JLabel ProductPriceLabel;
	
	/** The Product picture label. */
	public static JLabel ProductPictureLabel;
	
	/** The Order label. */
	public static JLabel OrderLabel;
	
	/** The Order label2. */
	public static JLabel OrderLabel2;
	
	/** The Order label3. */
	public static JLabel OrderLabel3;
	
	/** The Product id text field. */
	public static JTextField ProductIDTextField = null;
	
	/** The Product name text field. */
	public static JTextField ProductNameTextField=null;
	
	/** The Product color text field. */
	public static JTextField ProductColorTextField = null;
	
	/** The Product size text field. */
	public static JTextField ProductSizeTextField = null;
	
	/** The Product use text field. */
	public static JTextField ProductUseTextField = null;
	
	/** The Product price text field. */
	public static JTextField ProductPriceTextField = null;
	
	/** The Product picture text field. */
	public static JTextField ProductPictureTextField = null;
	
	/** The Addbutton. */
	public static JButton Addbutton;
	
	/** The displaypidbutton. */
	public static JButton displaypidbutton;
	
	/** The displaypid2button. */
	public static JButton displaypid2button;
	
	/** The fc. */
	public static  JFileChooser fc;
	
	/** The Openbutton. */
	public static JButton Openbutton;
	
	/** The confirmation lable. */
	public static JLabel confirmationLable=null;
	
	/** The confirmation lable2. */
	public static JLabel confirmationLable2=null;
	
	/** The confirmation lable3. */
	public static JLabel confirmationLable3=null;
	
	/** The picture. */
	public static JLabel picture=null;
	
	/** The back button. */
	public static JButton backButton = new JButton("Back");
	
	/** The s. */
	public String s=null;
	
	/** The Ecatalog. */
	private Catalog Ecatalog;
	
	/** The Ccatalog. */
	private CatalogControl Ccatalog;
	
	/** The pc. */
	private ProductControl pc;
	
	/** The Display button. */
	public static JButton DisplayButton;
	
	/** The Display2 button. */
	public static JButton Display2Button;
	
	/** The text area. */
	public static JTextArea textArea;
	
	/** The text area_1. */
	public static JTextArea textArea_1;
	private JLabel label;
	public static JTextField textField;
	private JButton button;
	public static JTextArea textArea_2;
	private JComboBox<String> comboBox;

;
	
	/**
	 * Instantiates a new update catalog.
	 */
	public UpdateCatalog()
	{
		super();
		viewCatalog();
	}

	/**
	 * View catalog.
	 */
	public void viewCatalog(){
		this.setSize( 1500,765 ); //set Frame size
		this.setLayout(null); //set layout
		OrderLabel = new JLabel();     //set new label
		OrderLabel.setBounds(new Rectangle(380, 120, 283, 19));//set label's  size and location
		OrderLabel.setText("Please insert product details:");
		OrderLabel.setFont(new Font("Arial", Font.ITALIC,16)); //set label font and size
		ProductIDLabel = new JLabel();     //set new label
		ProductIDLabel.setBounds(new Rectangle(450,170, 113, 18));//set label's  size and location
		ProductIDLabel.setText("Product ID:");  
		ProductIDLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		ProductNameLabel = new JLabel();     //set new label
		ProductNameLabel.setBounds(new Rectangle(450, 200, 113, 19));//set label's  size and location
		ProductNameLabel.setText("Product Name:"); 
		ProductNameLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		ProductColorLabel = new JLabel();     //set new label
		ProductColorLabel.setBounds(new Rectangle(450, 231, 113, 18));//set label's  size and location
		ProductColorLabel.setText("Product Color:"); 
		ProductColorLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		ProductSizeLabel = new JLabel();     //set new label
		ProductSizeLabel.setBounds(new Rectangle(450, 260, 113, 18));//set label's  size and location
		ProductSizeLabel.setText("Product Size:");  
		ProductSizeLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		ProductUseLabel = new JLabel();     //set new label
		ProductUseLabel.setBounds(new Rectangle(450, 290, 143, 18));//set label's  size and location
		ProductUseLabel.setText("Product Use:");  
		ProductUseLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		ProductPriceLabel = new JLabel();     //set new label
		ProductPriceLabel.setBounds(new Rectangle(450, 320, 143, 18));//set label's  size and location
		ProductPriceLabel.setText("Product Price:");  
		ProductPriceLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		ProductPictureLabel = new JLabel();     //set new label
		ProductPictureLabel.setBounds(new Rectangle(450, 350, 113, 18));//set label's  size and location
		ProductPictureLabel.setText("Product Picture:"); 
		ProductPictureLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		OrderLabel2 = new JLabel();     //set new label
		OrderLabel2.setBounds(new Rectangle(20, 81, 283, 19));//set label's  size and location
		OrderLabel2.setText("Click Display button to view products in Database"); 
		OrderLabel2.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		OrderLabel3 = new JLabel();     //set new label
		OrderLabel3.setBounds(new Rectangle(871, 170, 283, 19));//set label's  size and location
		OrderLabel3.setText("Click Display button to view products in catalog:"); 
		OrderLabel3.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		
		this.add(ProductIDLabel, null);   //add label to the panel
		this.add(ProductNameLabel, null);   //add label to the panel
		this.add(ProductColorLabel, null);   //add label to the panel
		this.add(ProductSizeLabel, null);   //add label to the panel
		this.add(ProductUseLabel, null);   //add label to the panel
		this.add(ProductPriceLabel, null);   //add label to the panel
		this.add(ProductPictureLabel, null);   //add label to the panel
		this.add(OrderLabel, null);   //add label to the panel
		this.add(OrderLabel2, null);   //add label to the panel
		this.add(OrderLabel3, null);   //add label to the panel
		backButton.setBounds(681, 583, 150, 23);   //set button's size and location
		add(backButton);          // add button to the panel
		
		ProductIDTextField = new JTextField(); 
		ProductIDTextField.setBounds(new Rectangle(590,170, 127, 17)); //set field's  size and location
		this.add(ProductIDTextField, null);   // add the field to the panel
		
		ProductNameTextField = new JTextField(); 
		ProductNameTextField.setBounds(new Rectangle(590,200, 127, 17)); //set field's  size and location
		this.add(ProductNameTextField, null);   // add the field to the panel
		
	/*	ProductColorTextField = new JTextField();
		ProductColorTextField.setBounds(new Rectangle(590, 230, 127, 17)); //set field's  size and location
		this.add(ProductColorTextField, null);   // add the field to the panel
	*/	
		
		comboBox = new JComboBox<String>();
		comboBox.setBounds(590, 231, 127, 20);
		comboBox.addItem("White");
		comboBox.addItem("Black");
		comboBox.addItem("Blue");
		comboBox.addItem("Green");
		comboBox.addItem("Red");
		comboBox.addItem("Yellow");
		comboBox.addItem("Pink");
		comboBox.addItem("Brown");
		comboBox.addItem("Purple");
		comboBox.addItem("Orange");
		comboBox.addItem("Grey");
		add(comboBox);
		ProductSizeTextField = new JTextField(); 
		ProductSizeTextField.setBounds(new Rectangle(590, 260, 127, 17)); //set field's  size and location
		this.add(ProductSizeTextField, null);   // add the field to the panel
		
		ProductUseTextField = new JTextField();   
		ProductUseTextField.setBounds(590, 290, 127, 17);//set field's size and location
		this.add(ProductUseTextField,null); // add the field to the panel
		
		ProductPriceTextField = new JTextField();  
		ProductPriceTextField.setBounds(590, 320, 127, 17);//set field's size and location
		this.add(ProductPriceTextField,null); // add the field to the panel	
		
		ProductPictureTextField = new JTextField();  
		ProductPictureTextField.setBounds(590, 349, 127, 17);//set field's size and location
		this.add(ProductPictureTextField,null); // add the field to the panel
	
		picture = new JLabel("");
		picture.setHorizontalAlignment(SwingConstants.CENTER);
		final JFileChooser fc = new JFileChooser();
		Openbutton = new JButton("browse");             
		Openbutton.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				Component aComponent = null;
				int returnVal = fc.showOpenDialog(aComponent);
				File f = fc.getSelectedFile();
	            s =  f.getPath();
	            System.out.println(s);
	            ProductPictureTextField.setText(s);
	            
	            picture.setIcon(new ImageIcon(s));	
			}
		});
		Openbutton.setBounds(727, 351, 77, 18);      //set button size
		add(Openbutton);

		
		picture.setBounds(475, 379, 313, 203);
		add(picture);
		
		
		
		Addbutton = new JButton("Add to catalog");            
		Addbutton.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				Ecatalog=new Catalog();
				Ecatalog.setProductId(ProductIDTextField.getText());
				Ecatalog.setProductName(ProductNameTextField.getText());
				Ecatalog.setProductColor((String)comboBox.getSelectedItem());
				Ecatalog.setProductSize(ProductSizeTextField.getText());
				Ecatalog.setProductUse(ProductUseTextField.getText());
				Ecatalog.setProductPrice(ProductPriceTextField.getText());
				Ecatalog.setProductPicture(s);
				System.out.println(s);

				Ccatalog=new CatalogControl();
				if(Ccatalog.CheckDetails(Ecatalog.getProductId(),Ecatalog.getProductName(),Ecatalog.getProductColor(),Ecatalog.getProductSize(),Ecatalog.getProductUse(),Ecatalog.getProductPrice(),Ecatalog.getProductPicture()))
				{
					Ccatalog.AddToCatalog(Ecatalog.getProductId(),Ecatalog.getProductName(),Ecatalog.getProductColor(),Ecatalog.getProductSize(),Ecatalog.getProductUse(),Ecatalog.getProductPrice(),Ecatalog.getProductPicture());
					confirmationLable.setVisible(true);
				}
				 else{ 
					 confirmationLable.setVisible(true);
					 confirmationLable.setText("Please fill all the fields!!");
				 }
				 
			}
		});
		
		Addbutton.setBounds(488, 583, 150, 23);      //set button size
		add(Addbutton);                            //add button to the panel
		displaypidbutton = new JButton("Display");            
		displaypidbutton.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				pc=new ProductControl();
				pc.ShowProducts();
			
			}
		});
		displaypidbutton.setBounds(58, 111, 150, 23);      //set button size
		add(displaypidbutton);
		
		displaypid2button = new JButton("Display");            
		displaypid2button.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				pc=new ProductControl();
				pc.ShowProductsInCatalog();
		
			}
		});
		
		displaypid2button.setBounds(930, 199, 150, 23);      //set button size
		add(displaypid2button);
		
		confirmationLable = new JLabel("");           // set result label
		confirmationLable.setBounds(511, 632, 350, 67);  // set label's size and location
		add(confirmationLable);
		confirmationLable.setVisible(false); 
		
		textArea = new JTextArea();
		textArea.setBackground(Color.LIGHT_GRAY);
		textArea.setEditable(false);
		textArea.setBounds(918, 235, 162, 253);
		add(textArea);
		
		textArea_1 = new JTextArea();
		textArea_1.setBackground(Color.LIGHT_GRAY);
		textArea_1.setEditable(false);
		textArea_1.setBounds(58, 145, 203, 253);
		add(textArea_1);
		
		label = new JLabel("Enter Product Number To Search");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(new Color(75, 0, 130));
		label.setFont(new Font("Tahoma", Font.BOLD, 13));
		label.setBounds(46, 433, 218, 34);
		add(label);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(80, 478, 86, 20);
		add(textField);
		
		button = new JButton("Search Product");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pc=new ProductControl();
				if(! pc.checkProdId(textField.getText()) ){
					textArea_2.setText("Pleae Fill The Search Field !!");
					textArea_2.setVisible(true);
				}
				else {
					textArea_2.setText("");
					pc.searcProduct(textField.getText());
					textArea_2.setVisible(true);
					
				}
			}});
	
		button.setFont(new Font("Tahoma", Font.PLAIN, 13));
		button.setBounds(175, 473, 142, 29);
		add(button);
		
		textArea_2 = new JTextArea();
		textArea_2.setEditable(false);
		textArea_2.setBackground(Color.LIGHT_GRAY);
		textArea_2.setBounds(58, 512, 236, 227);
		add(textArea_2);
		
		
		


			
		}
	}
